﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface ICKCateService : IAppService
    {
        List<CKCate> GetList(string CateID);
        void UpdateExplort(string CateID);
        List<CkCateInfo> GetCKCateInfoList(string CateID);
        bool BatchInsert(List<CKCate> list);
        void Add(AddCKCateInput input);
        void Update(UpdateCKCateInput input);
        void Delete(string id );

        CKCate GetModel(string Code); 

        PagedData<CKCate> GetPageData(Pagination page, int IsExplort, string CateID, string Code);
    }

    public class CKCateService : AppServiceBase<CKCate>, ICKCateService
    {
        public CKCateService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<CKCate> GetList(string CateID)
        {
            var q = this.Query;
            
            if (CateID.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.CateID.Contains(CateID));
            }

            var ret = q.ToList();
            return ret;
        }

        

        public void Add(AddCKCateInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateCKCateInput input)
        {
            this.UpdateFromDto(input);
        }

        public CKCate GetModel(string Code)
        {
            return  this.Query.Where(a => a.Code == Code).FirstOrDefault();
        }

        public void Delete(string id)
        {
            this.DbContext.Delete<CKCate>(a => a.Id == id);
        }


        public PagedData<CKCate> GetPageData(Pagination page, int IsExplort, string CateID, string Code)
        {
            var q = this.DbContext.Query<CKCate>();
            if(!string.IsNullOrEmpty(CateID))
            {
                q = q.WhereIfNotNullOrEmpty(CateID, a => a.CateID == CateID);
            }
            if(!string.IsNullOrEmpty(Code))
            {
                q = q.WhereIfNotNullOrEmpty(Code, a => a.Code == Code);
            }            
            if(IsExplort > -1)
            {
                q = q.Where(a => a.IsExplort == IsExplort);
            }
            q = q.OrderByDesc(a=>a.CreateDate);
            PagedData<CKCate> pagedData = q.TakePageData(page);
            return pagedData;
        }

       
        public List<CkCateInfo> GetCKCateInfoList(string CateID)
        {
            string sql = "select Code,f_pro_sort_name(CateID) as CateName from ck_cate where IsExplort=0 and CateID='" + CateID + "';  ";
            List<CkCateInfo> list = this.DbContext.SqlQuery<CkCateInfo>(sql).ToList();
            return list;
        }


        public void UpdateExplort(string CateID)
        {
            this.DbContext.Update<CKCate>(a => a.CateID == CateID, a => new CKCate()
            {
                IsExplort = 1
            });
        }


        public bool BatchInsert(List<CKCate> list)
        {
            bool v = false;

            string DbType = Globals.Configuration["db:DbType"].ToString();
            string ConnString = Globals.Configuration["db:ConnString"].ToString();
            if (DbType == "MySql")
            {
                using (MySqlContext context = new MySqlContext(new MySqlConnectionFactory(ConnString)))
                {
                    try
                    {
                        context.Session.BeginTransaction();

                        foreach (var item in list)
                        {                          
                            context.Insert<CKCate>(item);
                        }
                        context.Session.CommitTransaction();
                    }
                    catch
                    {
                        if (context.Session.IsInTransaction)
                            context.Session.RollbackTransaction();
                        throw;
                    }
                }
            }

             
            return v;
        }


    }

}
